<?php
/**
 * Created by PhpStorm.
 * User: alice
 * Date: 2015/12/28
 * Time: 12:26
 */

/**
 * Class Involved_OrderModel
 */

class Involved_OrderModel extends BaseModel{

    protected $table = 'order';


    /**
     * 获取订单列表
     * @param $user_id
     * @param $page
     * @return mixed
     * @author alice
     * @date 2016-2-18
     */
    public function GetOrderList($user_id,$page)
    {
        $offset = ($page-1)* HostEnum::PAGE_NUM;

//        return  DB::table($this->table)
//                ->select('order.order_id','order.order_sn','order.order_time','order.amount','order.order_status',
//                        'order_product.product_id',
//                        'logistics_status.logistics_status')
//                ->LeftJoin('order_product','order.order_id','=','order_product.order_id')
//                ->LeftJoin('logistics_status','order.order_id','=','logistics_status.order_id')
//                ->groupBy('order_product.order_id')
//                ->where('user_id',$user_id)
//                ->skip($offset)->take(HostEnum::PAGE_NUM)
//                ->get();

        return  DB::table($this->table)
                ->select('order.order_id','order.order_sn','order.order_time','order.amount','order.order_status',
                        'logistics_status.log_status')
                ->LeftJoin('logistics_status','order.order_id','=','logistics_status.order_id')
                ->where('user_id',$user_id)
                ->skip($offset)->take(HostEnum::PAGE_NUM)
                ->get();


    }

    /**
     * 获取订单详情
     * @param $order_id
     * @return mixed
     */
    public function GetOrderDetail($order_id)
    {
        return  DB::table($this->table)
                ->select('order.order_id','order.order_sn','order.order_status','trade_num','amount','order.order_time','order.address_id','order.user_id','order.supplier_id','order.pay_time','deal_time','send_time',
                        'order_product.product_id','order_product.number','supplier.supplier_name','user.user_name','supplier.phone')
                ->LeftJoin('order_product','order.order_id','=','order_product.order_id')
            ->LeftJoin('user','order.user_id','=','user.user_id')
            ->LeftJoin('supplier','order.supplier_id','=','supplier.supplier_id')
                ->where('order.order_id',$order_id)
                ->first();
    }
    public function GetOrderDetail1($order_id)
    {
        return  DB::table($this->table)
            ->select('order.order_id','order.order_sn','order.order_status','order.trade_num','order.amount','order.order_time'
                ,'order.address_id','order.pay_time','order.payment_id','order.start_amount','order_product.product_id')
            ->LeftJoin('order_product','order.order_id','=','order_product.order_id')
            ->where('order.order_id',$order_id)
            ->first();
    }
    /**
     * 获取订单信息
     * @param $order_id
     * @return mixed
     */
    public function ConfirmOrder($order_id)
    {
        return  DB::table($this->table)->where('order_id',$order_id)->get();
    }

    /**
     * 取消订单
     * @param $order_id
     * @param $user_id
     * @return mixed
     * @throws Exception
     */
    public function CancelOrder($order_id)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        #是否可以取消订单
        if ($order_info->order_status > OrderEnum::STATUS_DELIVERY)
            throw new Exception('订单状态不正确', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_CANCEL));
    }
    /**
     * 确认收货
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function ConfirmReceipt($order_id)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_RECEIVE,'deal_time'=>time() ));
    }
    /**
     * 修改价格
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function ChangePrice($order_id,$amount)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('amount' => $amount ));
    }

    /**
     * 确认发货
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function SendGoods($order_id)
    {
        #是否我的订单
        $order_info = DB::table($this->table)->where(['order_id' => $order_id])->first();
        if(!$order_info)
            throw new Exception('订单不存在', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_DELIVERY,'send_time'=>time() ));
    }
    /**
     * 删除订单
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function DeleteOrder($order_id)
    {
        $order_info = $this->ConfirmOrder($order_id);
        if ($order_info->order_status != OrderEnum::STATUS_RECEIVE)
            throw new Exception('订单状态不正确', ProfessionErrorCodeEnum::ERROR_DB_UPDATE);

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_DELETE));
    }
    /**
     * Created by PhpStorm.
     * 买(卖)家全部订单（待付款，待收货，已完成）
     * User: liuyuchao
     * Date: 2016/5/7
     * Time: 14:06
     */
    public function OrderSelect($user_id,$supplier_id,$status,$page)
    {
        $offset = ($page-1) * HostEnum::PAGE_NUM;
        if($user_id>0) {
            if($status==1){//待付款
                return  DB::table($this->table)
                    ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status',
                        'product.product_name','product.price','product.product_img','product.spec','supplier.phone')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->leftJoin('supplier','order.supplier_id','=','supplier.supplier_id')
                    ->where(['user_id' =>$user_id, 'order_status' => 1])
                    ->orderBy('order_time','desc')
                     ->skip($offset)->take(HostEnum::PAGE_NUM)
                    ->get();
            }elseif($status==2){//待发货
                return  DB::table($this->table)
                    ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status'
                        ,'product.product_name','product.price','product.product_img','product.spec','supplier.phone')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->leftJoin('supplier','order_product.supplier_id','=','supplier.supplier_id')
                    ->where(['user_id' => $user_id, 'order_status' => 2])
                    ->orderBy('order_time','desc')
                     ->skip($offset)->take(HostEnum::PAGE_NUM)
                    ->get();
            }elseif($status==3){//待收货
                return  DB::table($this->table)
                    ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status',
                       'product.product_name','product.price','product.product_img','product.spec','supplier.phone')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->leftJoin('supplier','order_product.supplier_id','=','supplier.supplier_id')
                    ->where(['user_id' => $user_id, 'order_status' => 4])
                    ->orderBy('order_time','desc')
                     ->skip($offset)->take(HostEnum::PAGE_NUM)
                    ->get();
            }else{//全部
                return  DB::table($this->table)
                    ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status',
                       'product.product_name','product.price','product.product_img','product.spec','supplier.phone')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->leftJoin('supplier','order_product.supplier_id','=','supplier.supplier_id')
                    ->where('user_id',$user_id)
                    ->orderBy('order_time','desc')
                     ->skip($offset)->take(HostEnum::PAGE_NUM)
                    ->get();
            }
        }
       else{
           if($status==1){//待付款
               return  DB::table($this->table)
                   ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status','order.supplier_id',
                      'product.product_name','product.price','product.product_img','product.spec')
                   ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                   ->leftJoin('product','order_product.product_id','=','product.product_id')
                   ->where(['order.supplier_id' =>$supplier_id, 'order_status' => 1])
                   ->orderBy('order_time','desc')
                    ->skip($offset)->take(HostEnum::PAGE_NUM)
                   ->get();
           }elseif($status==2){//待发货
               return  DB::table($this->table)
                   ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status','order.supplier_id',
                     'product.product_name','product.price','product.product_img','product.spec')
                   ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                   ->leftJoin('product','order_product.product_id','=','product.product_id')
                   ->where(['order.supplier_id' =>$supplier_id, 'order_status' => 2])
                   ->orderBy('order_time','desc')
                    ->skip($offset)->take(HostEnum::PAGE_NUM)
                   ->get();
           }elseif($status==3){//已发货
               return  DB::table($this->table)
                   ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status','order.supplier_id',
                     'product.product_name','product.price','product.product_img','product.spec')
                   ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                   ->leftJoin('product','order_product.product_id','=','product.product_id')
                   ->where(['order.supplier_id' =>$supplier_id, 'order_status' => 4])
                   ->orderBy('order_time','desc')
                    ->skip($offset)->take(HostEnum::PAGE_NUM)
                   ->get();
           }else{//全部
               return  DB::table($this->table)
                   ->select('order.order_id','order.order_sn','order.user_id','order.amount','order_product.product_id','order_product.number','order.order_status','order.supplier_id',
                     'product.product_name','product.price','product.product_img','product.spec')
                   ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                   ->leftJoin('product','order_product.product_id','=','product.product_id')
                   ->where('order.supplier_id',$supplier_id)
                   ->orderBy('order_time','desc')
                    ->skip($offset)->take(HostEnum::PAGE_NUM)
                   ->get();
           }
       }

    }


    /**
     * Created by PhpStorm.
     * 我的订单（全部，待付款，待收货）
     * User: liuyuchao
     * Date: 2016/07/01
     * Time: 14:06
     */
    public function OrderSelectPc($user_id,$status)
    {
            if($status==1){//待付款
                return  DB::table($this->table)
                    ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount','order_product.product_id','order.order_status',
                        'product.product_name','product.product_img')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->leftJoin('supplier','order.supplier_id','=','supplier.supplier_id')
                    ->where(['user_id' =>$user_id, 'order_status' => 1])
                    ->orderBy('order_time','desc')
                    ->get();
            }elseif($status==3){//待收货
                return  DB::table($this->table)
                    ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount','order_product.product_id','order.order_status',
                        'product.product_name','product.product_img')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->leftJoin('supplier','order_product.supplier_id','=','supplier.supplier_id')
                    ->where(['user_id' => $user_id])
                    ->where(function($query){
                        $query->where('order_status','=' ,2)
                            ->orWhere(function($query){
                                $query->where('order_status','=' ,4);
                            });
                    })
                    ->orderBy('order_time','desc')
                    ->get();
            }else{//全部
                return  DB::table($this->table)
                    ->select('order.order_id','order.payment_id','order.order_time','order.order_sn','order.user_id','order.amount','order.order_status',
                        'order_product.product_id','product.product_name','product.product_img')
                    ->leftJoin('order_product','order.order_id','=','order_product.order_id')
                    ->leftJoin('product','order_product.product_id','=','product.product_id')
                    ->where('user_id',$user_id)
                    ->where('order_status', '!=', 7)
                    ->orderBy('order_time','desc')
                    ->get();
            }
        }




//更改状态
    public function updatestatus($TradeNum){
        return DB::table($this->table)->where(['trade_num' => $TradeNum])->update(array('order_status' => OrderEnum:: STATUS_PAYED,'pay_time'=>time() ));


    }
    public function UpdateTradeNum($TradeNum){
        $num = 'MTW'.time().rand(100,999);
        return DB::table($this->table)->where(['trade_num' => $TradeNum])->update(array('trade_num' =>$num));


    }
//搜索订单字段提交给支付
    public function SelectOrder($order_id){
        return  DB::table($this->table)
            ->select('order_id','order_sn','order_time','amount','trade_num')
            ->where('order_id',$order_id)
            ->first();


    }
    //POS机订单
    public function posorderinfo($order_id){
        return  DB::table($this->table)
            ->select('order_id','order_sn','order_time','amount','trade_num')
            ->where('order_id',$order_id)
            ->first();


    }

    public function OrderList()
    {
        return DB::table($this->table)
            ->select('user_name','order_sn','order.amount','order_time','order_status','pay_name')
            ->leftjoin('user','order.user_id','=','user.user_id')
            ->leftjoin('order_pay','order.payment_id','=','order_pay.pay_id')
            ->get();
    }
    //订单超时
    public function Order_Expired(){
        $status = 1;
        return  DB::table($this->table)
            ->select('order_id','order_time')
            ->where('order_status',$status)
            ->get();

    }

    /**
     * 删除订单PC
     * @param $order_id
     * @return mixed
     * @throws Exception
     */
    public function DeleteOrderPc($order_id)
    {

        return  DB::table($this->table)->where('order_id',$order_id)->update(array('order_status' => OrderEnum::STATUS_DELETE));
    }



}